package controller

import (
	"mini-app/internal/app/admin/service"
	"mini-app/internal/models"
	"strconv"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

type Banner struct{}

func (Banner) Create(c *gin.Context) {
	var params models.Banner
	c.ShouldBindBodyWithJSON(&params)
	id, err := service.Banner{}.Create(c.Request.Context(), params)
	response.JSON(c, gin.H{
		"id": id,
	}, err)
}
func (Banner) Update(c *gin.Context) {
	var params models.Banner
	c.ShouldBindBodyWithJSON(&params)
	err := service.Banner{}.Update(c.Request.Context(), params.ID, params)
	response.JSON(c, nil, err)
}
func (Banner) Delete(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWithJSON(&params)
	err := service.Banner{}.Delete(c.Request.Context(), params.ID)
	response.JSON(c, nil, err)
}
func (Banner) Detail(c *gin.Context) {
	id := c.Query("id")
	detail := service.Banner{}.Detail(c.Request.Context(), id)
	response.JSON(c, detail, nil)
}
func (Banner) List(c *gin.Context) {
	name := c.Query("name")
	position, _ := strconv.Atoi(c.Query("position"))
	status, _ := strconv.Atoi(c.Query("status"))
	startTime, _ := strconv.Atoi(c.Query("start_time"))
	endTime, _ := strconv.Atoi(c.Query("end_time"))
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	list, total := service.Banner{}.List(
		c.Request.Context(),
		name,
		position,
		int64(startTime), int64(endTime),
		status,
		page, limit,
	)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}
func (Banner) Online(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWithJSON(&params)
	err := service.Banner{}.Online(c.Request.Context(), params.ID)
	response.JSON(c, nil, err)
}
func (Banner) Offline(c *gin.Context) {
	var params struct {
		ID string `json:"id"`
	}
	c.ShouldBindBodyWithJSON(&params)
	err := service.Banner{}.Offline(c.Request.Context(), params.ID)
	response.JSON(c, nil, err)
}
